import { useState } from 'react'
import SecureStorage from 'react-native-secure-storage'

export default function (key, initValue) {
  const [value, setValue] = useState(() => {
    const localValue = SecureStorage.getItem(key)
    return localValue ? JSON.parse(localValue) : initValue
  })

  const setStorage = params => {
    setValue(params)
    SecureStorage.setItem(key, JSON.stringify(params))
  }

  return [value, setStorage]
}
